﻿Chenxin.UI.ContactsMgr = Chenxin.extend( Chenxin.UI.PageCommon,{
    autoInitForm:false,
    actionUrl:'/service/contactsMgr.ashx',
    createSearch:function(){
        //人事
        var srhPersonnel = new Chenxin.UI.PersonnelSearch({
            scope:this,
            afterGetParamJson:function(json){
                json.op="SearchPersonnel";
                
                if( $("#hidConditionId").val() == "" ){
                    if( json.divDeparts ){
                        json.DepartId = json.divDeparts;
                        delete json.divDeparts;
                    }
                }else{
                    json.UserId = $("#hidConditionId").val();
                }
            }
        });
        //个人
		var srhPersonal = new Chenxin.UI.PersonalSearch({
            scope:this,
            afterGetParamJson:function(json){
                json.op="SearchPersonal";
                if($("#hidConditionId").val() != ""){
                    json.RecordId = $("#hidConditionId").val();
                }
            }
        });        
        this.search1 = srhPersonal;
        this.activeSearch = srhPersonnel;
        return srhPersonnel;
	},
	createGrid:function(){
	    return new Chenxin.UI.Pagination({
	        type:"post",
			actionUrl:this.actionUrl,
			data:{op:"SearchPersonnel"},
			pageSize:51,
			renderTo: 'house_table'
		});
	},
	createForm:function(){
        return new Chenxin.UI.EditContactForm({
            scope:this,
            afterGetFormJson:function(json){
                
            }
        });
    },
    setSearchMatch:function(dlContainer){
        var objPg = this;
        this.intervalId = window.setInterval(function() { $("#" + dlContainer).hide(); objPg.clearTime() ; }, 300);
    },
    clearTime:function(){
        window.clearInterval(this.intervalId);
    },
	initEvent:function(){
	    var objPg = this;
	    /******************事件：人事Tab点击******************/
	    $("#aPersonnelContacts").click(function(){
	        $("#divPersonalSearch").hide();
	        $("#divPersonnelSearch").show();
	        $("#divButton").hide();
	        $("#hidType").val("Personnel");
	        $("#hidConditionId").val("");
	        $("#hidConditionType").val("");
	        var op={op:"SearchPersonnel"};
	        
	        objPg.activeSearch = objPg.search;
	        objPg.search.reset();
	        objPg.reloadGrid(op);
	    });
	    /******************事件：个人Tab点击******************/
	    $("#aPersonalContacts").click(function(){
	        $("#hidConditionId").val("");
	        $("#hidConditionType").val("");
	        $("#txtPersonalConditions").val("");
	        $("#divPersonnelSearch").hide();
	        $("#divPersonalSearch").show();
	        $("#divButton").show();
	        $("#hidType").val("Personal");
	        var op={op:"SearchPersonal"};
	        
	        objPg.activeSearch = objPg.search1;
	        
	        objPg.activeSearch.reset();
	        objPg.reloadGrid(op);
	    });
	    /******************事件：首字母点击事件******************/
	    $(".initial").each(function() {
	        $(this).bind("click", function() {
	            var strInitial = $(this).find("span").text();
	            
	            var json = objPg.activeSearch.getParamJson();
	            if ("全部" == strInitial)
	                delete json.Initial;
	            else
	                json.Initial = strInitial;

	            //检索数据
	            objPg.grid.reload(json);
	        });	    
	    });
	    /******************事件：添加联系人******************/
	    $("#aAddContact").click(function(){
	        $("#RecordId").val("");
	        
	        objPg.form.reset();
			objPg.form.setTitle('添加联系人');
			objPg.form.show();
	    });
	    /******************事件：编辑联系人******************/
	    $(".clsEdit").live("click",function(){
	    
            var data = {};          
            var dataContainer = $(this).parents("div:first");
            
            data.RecordId = dataContainer.children("input[name=UserId]").val();
            data.UserName = objPg.getChildItem(dataContainer,0);
            data.Company = objPg.getChildItem(dataContainer,1);
            data.Depart = objPg.getChildItem(dataContainer,2);
            data.Position = objPg.getChildItem(dataContainer,3);
            data.Mobile = objPg.getChildItem(dataContainer,4);
            data.Tel = objPg.getChildItem(dataContainer,5);
            data.Email = objPg.getChildItem(dataContainer,6);
            
            objPg.form.reset();
	        objPg.form.currentModel=data;
	        objPg.form.fitData(data);
	        objPg.form.setTitle('编辑联系人');
	        objPg.form.show();
	    });
	    /**************************事件：删除事件**************************/
	    $(".clsDel").live("click",function(){
	        var RecordId;
	        var dataContainer = $(this).parents("div:first");
	        RecordId = dataContainer.children("input[name=UserId]").val();
	        
	        Chenxin.ajax({
		        type: "post",
		        dataType: "json",
		        url: objPg.actionUrl,
		        data: {op:'Del',RecordId:RecordId},
		        onSuccess: function(data,msg) {
		            alert(msg);
                    var json = objPg.activeSearch.getParamJson();
                    json.op = "SearchPersonal";
                    objPg.reloadGrid(json);
		        }
		    });
		    
	    });
	    /**************************事件：转入黑名单事件**************************/
	    $(".clsBlackList").live("click",function(){
	        
	        var dataContainer = $(this).parents("div:first");
	        var RecordId = dataContainer.children("input[name=UserId]").val();
	        var Type = $("#hidType").val();
	        
	        Chenxin.ajax({
		        type: "post",
		        dataType: "json",
		        url: objPg.actionUrl,
		        data: {op:'ToBlackList',RecordId:RecordId,Type:Type},
		        onSuccess: function(data,msg) {
                    alert(msg);
		        }
		    });
		    
	    });
	},
	getChildItem:function(objContainer,idx){
	   var valItem = objContainer.children("p:eq(" + idx + ")").text();
	   var lsVal = valItem.split("：");
	   return lsVal[1];
	},
	initComponent:function(){
		Chenxin.UI.ContactsMgr.superclass.initComponent.call(this);
	}
});

/**
*人事查询面板
*/
Chenxin.UI.PersonnelSearch=Chenxin.extend(Chenxin.UI.SearchCommon,{
    id:'divPersonnelSearch',
    createComboList:function(){
		var cmbStruct = new Chenxin.UI.StructComboList();
		var cmbList = cmbStruct.comboList;
		return cmbList;
	},
    initEvent:function(){
        var objSrh = this;
        /**************************事件：人事联系人检索**************************/
        $("#btPersonnelSearch").click(function(){
            objSrh.scope.reloadGrid();
        });     
        /*************************事件：条件输入框匹配(人事)************************/
        $("#txtPersonnelConditions").keyup(function(){

            var strCondition = $("#txtPersonnelConditions").val();
            var json = {op:"GetPersonnelCondition"}
            if(strCondition != ""){
                json.query = strCondition;
            }else{
                $("#hidConditionType").val("");
                $("#hidConditionId").val("");
                $("#dlMatchPersonnel").hide();
                return;
            }
            
            Chenxin.ajax({
		        type: "post",
		        dataType: "json",
		        url: '/service/contactsMgr.ashx',
		        data: json,
		        onSuccess: function(data) {
			        $("#dlMatchPersonnel").html(data);
                    $("#dlMatchPersonnel").show();
		        }
		    });
        });
        /*************************事件：失去焦点(人事)************************/
        $("#txtPersonnelConditions").blur(function(){
             objSrh.scope.setSearchMatch("dlMatchPersonnel");
        });
       /*************************事件：匹配层选择事件(人事)************************/
       $("#dlMatchPersonnel").live("click", function(event) {
            if (event.target.tagName == "DD") {
                $("#txtPersonnelConditions").val($(event.target).find("a").text());
                $("#hidConditionType").val($(this).find("input[name='hidType']").val());
                $("#hidConditionId").val($(this).find("input[name='hidId']").val());
            }else if(event.target.tagName == "A"){
                var objParent = $(event.target).parent();
                $("#txtPersonnelConditions").val($(event.target).text());
                $("#hidConditionType").val($(objParent).find("input[name='hidType']").val());
                $("#hidConditionId").val($(objParent).find("input[name='hidId']").val());
            }
        });
    }
});

/**
*个人查询面板
*/
Chenxin.UI.PersonalSearch=Chenxin.extend(Chenxin.UI.SearchCommon,{
    id:'divPersonalSearch',
    initEvent:function(){
        var objSrh = this;
        /**************************事件：个人联系人检索**************************/
        $("#btPersonalSearch").click(function(){
            objSrh.scope.reloadGrid(objSrh.getParamJson());
        });
        /*************************事件：条件输入框匹配(个人)************************/
        $("#txtPersonalConditions").keyup(function(){

            var strCondition = $("#txtPersonalConditions").val();
            var json = {op:"GetPersonalCondition"};
            if(strCondition != ""){
                json.query = strCondition;
            }else{
                $("#hidConditionType").val("");
                $("#hidConditionId").val("");
                $("#dlMatchPersonal").hide();
                return;
            }
            
            Chenxin.ajax({
		        type: "post",
		        dataType: "json",
		        url: '/service/contactsMgr.ashx',
		        data: json,
		        onSuccess: function(data) {
			        $("#dlMatchPersonal").html(data);
                    $("#dlMatchPersonal").show();
		        }
		    });
        });
        /*************************事件：失去焦点(个人)************************/
        $("#txtPersonalConditions").blur(function(){
             objSrh.scope.setSearchMatch("dlMatchPersonal");
        });
       /*************************事件：匹配层选择事件(个人)************************/
       $("#dlMatchPersonal").live("click", function(event) {
            if (event.target.tagName == "DD") {
                $("#txtPersonalConditions").val($(event.target).find("a").text());
                $("#hidConditionType").val($(this).find("input[name='hidType']").val());
                $("#hidConditionId").val($(this).find("input[name='hidId']").val());
            }else if(event.target.tagName == "A"){
                var objParent = $(event.target).parent();
                $("#txtPersonalConditions").val($(event.target).text());
                $("#hidConditionType").val($(objParent).find("input[name='hidType']").val());
                $("#hidConditionId").val($(objParent).find("input[name='hidId']").val());
            }
        });
    }
});

/**
*编辑个人通讯录
*/
Chenxin.UI.EditContactForm = new Chenxin.extend(Chenxin.UI.FormCommon,{
    actionUrl:'/service/contactsMgr.ashx',
	id:'jqmEditContacts',
	renderTo:'divEditContacts',
	winCls:'',
	jqmWidth:500,
	initFormValidator:function(){
	    ///////////////姓名///////////////
	    $("#UserName").formValidator({
	        validatorgroup:this.id,
	        onshow: "请输入姓名", 
	        onfocus: "请输入姓名", 
	        oncorrect: "OK"
	    }).inputValidator({
	        min: 1, empty: { leftempty: false, rightempty: true, emptyerror: "姓名不能为空！" }, onerror: "姓名不能为空！"
	    });
	    ///////////////电子邮件///////////////
	    $("#Email").formValidator({ 
	        validatorgroup:this.id,
	        onshow: "请输入Email", 
	        onfocus: "请输入Email", 
	        oncorrect: "OK"
	    }).regexValidator({
            regexp:"emailOrNull",
            datatype:"enum",
            onerror:"请输入正确的Email！"
        });
        //电话
        $("#Tel").formValidator({ 
            validatorgroup: this.id,
            onshow: "请输入电话！", 
            onfocus: "请输入电话", 
            oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#Tel").val() == "" && $("#Mobile").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话！"
        });
        //手机号码
        $("#Mobile").formValidator({ 
            validatorgroup: this.id,
            onshow: "请输入手机号！", 
            onfocus: "请输入手机号", 
            oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) { if (val == "") return true; return val.isMobile(); }, onerror: "请输入正确的手机号"
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#Tel").val() == "" && $("#Mobile").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话！"
        });
	},
	createForm:function(){
	    return formCommon.editContactForm('EditContact');
	},
	initEvent:function(){
	    var objForm = this;
	    /**************************事件：关闭弹出层事件**************************/
	    $("#btCloseEditContact").click(function(){
	        objForm.hide();
	    });
	    /**************************事件：提交事件**************************/
	    $("#btSaveEditContact").click(function(){
	        objForm.op = "Save";
	        objForm.save();
	    });
	},
	saveSucc:function(){
	    
	    var params = this.activeSearch.getParamJson();
	    params.op = "SearchPersonal";
	    //刷新
	    this.reloadGrid(params);
	},
	initComponent:function(){
	    Chenxin.UI.EditContactForm.superclass.initComponent.call(this);
	}
});

$(document).ready(function(){
    $('#container-1 > ul').tabs();
    $('#container-2 > ul').tabs();
    
    var pgContactsMgr = new Chenxin.UI.ContactsMgr();
});